<template>
  <div>
    <el-row type=flex>
      <el-col :span="16">
        <el-button @click="handleCreate" icon="el-icon-plus" type="primary">新建限时折扣活动</el-button>
      </el-col>
      <el-col :span="8">
        <el-input placeholder="请输入限时折扣活动名称" v-model="filterForm.title">
          <el-button @click="handleFilter" slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </el-col>
    </el-row>

    <el-table :data="event.list" stripe class="hui-mt-1">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form :model="scope.row" label-width="8em" label-position="left" inline class="hui-data-table-expand">
            <el-form-item label="活动标题:">{{ scope.row.title }}</el-form-item>
            <el-form-item label="开始时间:">{{ scope.row.startTime | moment("YYYY-MM-DD HH:mm:ss") }}</el-form-item>
            <el-form-item label="结束时间:">{{ scope.row.endTime | moment("YYYY-MM-DD HH:mm:ss") }}</el-form-item>
            <el-form-item label="生效日期限制:">{{ scope.row.eventAllWeekly ? '所有日期' : '一周内的某些天' }}</el-form-item>
            <el-form-item label="可生效日期:" v-if="!scope.row.eventAllWeekly">
              <el-tag v-for="item in scope.row.canEventWeekly" :key="item" type="primary" size="small" class="hui-mr-8px">
                <template v-if="item === 1">周一</template>
                <template v-else-if="item === 2">周二</template>
                <template v-else-if="item === 3">周三</template>
                <template v-else-if="item === 4">周四</template>
                <template v-else-if="item === 5">周五</template>
                <template v-else-if="item === 6">周六</template>
                <template v-else-if="item === 7">周日</template>
              </el-tag>
            </el-form-item>
            <el-form-item label="生效时段限制:">{{ scope.row.eventAllDaily ? '所有时段' : '一天内的某些时段' }}</el-form-item>
            <el-form-item label="可生效时段:" v-if="!scope.row.eventAllDaily">
              <el-tag v-for="item in scope.row.canEventDaily" :key="item" type="primary" size="small" class="hui-mr-8px">
                <template v-if="item === 0">0点 - 1点</template>
                <template v-else-if="item === 1">1点 - 2点</template>
                <template v-else-if="item === 2">2点 - 3点</template>
                <template v-else-if="item === 3">3点 - 4点</template>
                <template v-else-if="item === 4">4点 - 5点</template>
                <template v-else-if="item === 5">5点 - 6点</template>
                <template v-else-if="item === 6">6点 - 7点</template>
                <template v-else-if="item === 7">7点 - 8点</template>
                <template v-else-if="item === 8">8点 - 9点</template>
                <template v-else-if="item === 9">9点 - 10点</template>
                <template v-else-if="item === 10">10点 - 11点</template>
                <template v-else-if="item === 11">11点 - 12点</template>
                <template v-else-if="item === 12">12点 - 13点</template>
                <template v-else-if="item === 13">13点 - 14点</template>
                <template v-else-if="item === 14">14点 - 15点</template>
                <template v-else-if="item === 15">15点 - 16点</template>
                <template v-else-if="item === 16">16点 - 17点</template>
                <template v-else-if="item === 17">17点 - 18点</template>
                <template v-else-if="item === 18">18点 - 19点</template>
                <template v-else-if="item === 19">19点 - 20点</template>
                <template v-else-if="item === 20">20点 - 21点</template>
                <template v-else-if="item === 21">21点 - 22点</template>
                <template v-else-if="item === 22">22点 - 23点</template>
                <template v-else-if="item === 23">23点 - 24点</template>
              </el-tag>
            </el-form-item>
            <el-form-item label="折扣力度:">打 {{ scope.row.discount }} 折</el-form-item>
            <el-form-item label="参加用户限制:">{{ scope.row.eventAllUser ? '所有用户都可参加' : '部分用户可参加' }}</el-form-item>
            <el-form-item v-if="!scope.row.eventAllUser" label="可参加用户:">
              <el-tag v-for="item in scope.row.canEventUser" :key="item.cardId" size="small" type="primary" class="hui-mr-8px">{{ item.cardName }}</el-tag>
            </el-form-item>
            <el-form-item label="活动门店:">{{ scope.row.eventAllBranch ? '所有门店' : '部分门店' }}</el-form-item>
            <el-form-item v-if="!scope.row.eventAllBranch" label="活动门店列表:">
              <el-tag v-for="item in scope.row.canEventBranch" :key="item.branchId" size="small" type="primary" class="hui-mr-8px">{{ item.branchName }}</el-tag>
            </el-form-item>
            <el-form-item label="用户打标签:">
              <el-tag v-for="item in scope.row.userTags" :key="item.id" size="small" class="hui-mr-8px"># {{ item.label }} #</el-tag>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="活动标题 / 门店 / 状态">
        <template slot-scope="scope">
          <div>{{ scope.row.title }}</div>
          <div>{{ scope.row.eventAllBranch ? '所有门店' : '适用&nbsp;' + scope.row.canEventBranch.length + '&nbsp;门店' }}</div>
          <div>
            <el-tag v-if="scope.row.status === 0" size="small">未开始</el-tag>
            <el-tag v-else-if="scope.row.status === 1" type="success" size="small">进行中</el-tag>
            <el-tag v-else-if="scope.row.status === 2" type="info" size="small">已结束</el-tag>
            <el-tag v-else-if="scope.row.status === 3" type="danger" size="small">已下线</el-tag>
            <el-tag v-else type="danger" size="small">{{ scope.row.status }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="活动时间">
        <template slot-scope="scope">
          <div>开始: {{ scope.row.startTime | moment("YYYY-MM-DD HH:mm:ss") }}</div>
          <div>结束: {{ scope.row.endTime | moment("YYYY-MM-DD HH:mm:ss") }}</div>
        </template>
      </el-table-column>
      <el-table-column label="最后操作人">
        <template slot-scope="scope">
          <div>{{ scope.row.updateUser }}</div>
          <div>{{ scope.row.updateTime | moment("YYYY-MM-DD HH:mm:ss") }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200px">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="primary" size="mini" :disabled="scope.row.status === 2 || scope.row.status === 3" plain>编辑</el-button>
          <el-button @click="handleDelete(scope.row)" :disabled="scope.row.status === 2 || scope.row.status === 3" type="danger" size="mini" plain>下线</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页，start-->
    <el-pagination @current-change="handleCurrentChange" :current-page.sync="event.pages" :page-sizes="[10]" :page-size="10" layout="total, sizes, prev, pager, next" :total="event.total" background class="hui-mt-2"></el-pagination>
    <!--分页，end-->
  </div>
</template>

<script>
export default {
  data () {
    return {
      filterForm: {
        title: ''
      },
      loading: false,
      event: {
        pages: 0,
        total: 0,
        list: []
      }
    }
  },
  created () {
    this.mockData()
  },
  methods: {
    // 筛选事件
    handleFilter () {
      if (this.filterForm.title === '') {
        return false
      } else {
        this.loading = true
        console.log(this.filterForm)
        this.$loading()
        setTimeout(() => {
          this.$loading().close()
          this.loading = false
        }, 500)
      }
    },
    mockData () {
      let pages = this.event.pages
      let total = 100
      let list = []
      let tmp = [
        {
          id: 29999,
          title: '活动标题29999',
          startTime: 1518451200,
          endTime: 1519833599,
          status: 0,
          updateTime: 1518504734,
          updateUser: '黄倬, 13262268525',
          eventAllWeekly: false,
          canEventWeekly: [1, 2, 3, 4, 5],
          eventAllDaily: true,
          canEventDaily: [],
          discount: 85,
          eventAllUser: false,
          canEventUser: [
            {
              cardId: '99999',
              cardName: '会员卡名称1'
            },
            {
              cardId: '88888',
              cardName: '会员卡名称2'
            }
          ],
          eventAllBranch: false,
          canEventBranch: [
            {
              branchId: '123123',
              branchName: '门店名称1'
            },
            {
              branchId: '345345345',
              branchName: '门店名称2'
            }
          ],
          userTags: [
            {
              id: '12333333',
              label: '标签名称1'
            },
            {
              id: '888776',
              label: '标签名称2'
            }
          ]
        },
        {
          id: 29998,
          title: '活动标题29998',
          startTime: 1518451200,
          endTime: 1519833599,
          status: 1,
          updateTime: 1518504734,
          updateUser: '黄倬, 13262268525',
          eventAllWeekly: true,
          canEventWeekly: [],
          eventAllDaily: false,
          canEventDaily: [11, 12, 13],
          discount: 85,
          eventAllUser: true,
          canEventUser: [],
          eventAllBranch: true,
          canEventBranch: [],
          userTags: []
        },
        {
          id: 29997,
          title: '活动标题29997',
          startTime: 1518451200,
          endTime: 1519833599,
          status: 2,
          updateTime: 1518504734,
          updateUser: '黄倬, 13262268525',
          eventAllWeekly: false,
          canEventWeekly: [1, 2, 3, 4, 5],
          eventAllDaily: false,
          canEventDaily: [11, 12, 13],
          discount: 85,
          eventAllUser: true,
          canEventUser: [],
          eventAllBranch: false,
          canEventBranch: [
            {
              branchId: '123123',
              branchName: '门店名称1'
            },
            {
              branchId: '789789',
              branchName: '门店名称2'
            }
          ],
          userTags: [
            {
              id: '12333333',
              label: '标签名称1'
            },
            {
              id: '888776',
              label: '标签名称2'
            }
          ]
        },
        {
          id: 29996,
          title: '活动标题29996',
          startTime: 1518451200,
          endTime: 1519833599,
          status: 3,
          updateTime: 1518504734,
          updateUser: '黄倬, 13262268525',
          eventAllWeekly: false,
          canEventWeekly: [2],
          eventAllDaily: true,
          canEventDaily: [],
          discount: 85,
          eventAllUser: true,
          canEventUser: [],
          eventAllBranch: false,
          canEventBranch: [
            {
              branchId: '123123',
              branchName: '门店名称1'
            },
            {
              branchId: '654645',
              branchName: '门店名称2'
            }
          ],
          userTags: [
            {
              id: '12333333',
              label: '标签名称1'
            },
            {
              id: '888776',
              label: '标签名称2'
            }
          ]
        }
      ]
      tmp.map(element => {
        list.push(element)
      })
      this.event.list = list
      this.event.pages = pages + 1
      this.event.total = total
    },
    handleCreate () {
      this.$router.push({
        path: 'detail',
        query: {
          type: 'create'
        }
      })
    },
    // 编辑事件
    handleEdit (row) {
      let id = row.id
      this.$router.push({
        path: 'detail',
        query: {
          type: 'edit',
          id: id
        }
      })
    },
    handleDelete (row) {
      this.$confirm('确认下线这个活动么？下线后，活动将立即结束。', '下线确认', {
        confirmButtonText: '下线',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log('下线, 活动ID:' + row.id)
        this.$message({
          type: 'success',
          message: '活动已下线!'
        })
      }).catch(() => {})
    },
    // 分页事件
    handleCurrentChange () {
      console.log(this.event.pages)
      this.$loading()
      setTimeout(() => {
        this.$loading().close()
      }, 500)
    }
  }
}
</script>
